<template>
  <div class="tools-li">
    <transition name="el-zoom-in-top">
      <div
        v-show="isShow"
        :key="tooltipId"
        ref="customAlert"
        class="custom-demo"
        transiton="el-zoom-in-center"
      >
        <div class="custom-body">
          <div class="custom-header">
            <div class="user-image">
              <el-image class="user-tx" :src="user.avatarName === null ? userImage: user.avatarName">
                <div slot="error" class="image-slot" style="width: 50px;height: 60px">
                  <el-image :src="userImage" />
                </div>
              </el-image>
            </div>
            <div class="user-info">
              <div class="user-info-li">{{ user.nickName }}</div>
              <div class="user-info-li">{{ user.username }}</div>
            </div>
          </div>
          <div class="custom-button">
            <el-button type="text" class="custom-button-color" icon="el-icon-switch-button" @click="Logout">退出登录</el-button>
            <el-button v-if="user.username === 'admin'" class="custom-button-color" type="text" icon="el-icon-monitor" @click="toConsole">控制台</el-button>
          </div>
        </div>
      </div>
    </transition>
    <slot name="user" />
    <div v-if="systemList" class="custom-iframe">
      <iframe v-for="item in systemList" :key="item.id" :src="item.url+item.logoutUrl" />
    </div>
  </div>
</template>

<script>
import { generateId } from 'element-ui/src/utils/util'
import { mapGetters } from 'vuex'
import setImage from '@/assets/images/user.png'
import { queryAllSystem } from '@/api/system/usystem'
import Config from '@/settings'
import { layout } from '@/mixins/layout'
export default {
  name: 'PortalUserInformation',
  mixins: [layout],
  props: {
    trigger: {
      type: String,
      default: 'click',
      validator: value => ['click', 'focus', 'hover', 'manual'].indexOf(value) > -1
    }
  },
  data() {
    return {
      userImage: setImage,
      isShow: false,
      isLogin: false,
      systemList: []
    }
  },
  computed: {
    ...mapGetters([
      'user'
    ]),
    tooltipId() {
      return `el-custom-user-${generateId()}`
    }
  },
  mounted() {
    if (this.trigger === 'click' && undefined !== this.$refs.customAlert) { // click 事件
      this.$refs.customAlert.addEventListener('click', this.triggerOnClick)
    }
    if (this.trigger === 'hover' && undefined !== this.$refs.customAlert) {
      this.$refs.customAlert.addEventListener('mouseenter', this.triggerOpen)
      this.$refs.customAlert.addEventListener('mouseleave', this.triggerClose)
    }
  },
  destroyed() {
    if (this.trigger === 'click' && undefined !== this.$refs.customAlert) { // click 事件
      this.$refs.customAlert.removeEventListener('click', this.triggerOnClick)
    }
    if (this.trigger === 'hover' && undefined !== this.$refs.customAlert) {
      this.$refs.customAlert.removeEventListener('mouseenter', this.triggerOpen)
      this.$refs.customAlert.removeEventListener('mouseleave', this.triggerClose)
    }
  },
  methods: {
    triggerOnClick() {
      if (this.isShow) {
        this.isShow = false
      } else {
        this.isShow = true
      }
    },
    triggerOpen() {
      this.isShow = true
      this.$emit('callBack', this.isShow)
    },
    triggerClose() {
      this.isShow = false
      this.$emit('callBack', this.isShow)
    },
    Logout() {
      this.$confirm('确定注销并退出系统吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.userLoginOut()
      }).catch(() => {
        this.$message.success('取消成功！')
      })
    },
    toConsole() {
      window.open('/dashboard', '_blank')
    },
    userLoginOut() {
      const loading = this.$loading({
        lock: true,
        text: 'Loading'
      })
      queryAllSystem().then(res => {
        const { content } = res
        this.systemList = content
        this.$store.dispatch('LogOut').then(() => {
          if (Config.isCas) {
            setTimeout(() => {
              loading.close()
              window.location.href = Config.casLogout
            }, 600)
          } else {
            setTimeout(() => {
              loading.close()
              window.location.href = Config.casLogout
            }, 600)
            location.reload()
          }
        }).catch(() => {
          if (Config.isCas) {
            setTimeout(() => {
              window.location.href = Config.casLogout
              loading.close()
            }, 600)
            window.location.href = Config.casLogout
          } else {
            setTimeout(() => {
              window.location.href = Config.casLogout
              loading.close()
            }, 600)
            location.reload()
          }
        })
      }).catch(() => {
        this.$store.dispatch('LogOut').then(() => {
          if (Config.isCas) {
            loading.close()
            window.location.href = Config.casLogout
          } else {
            loading.close()
            location.reload()
          }
        }).catch(() => {
          if (Config.isCas) {
            loading.close()
            window.location.href = Config.casLogout
          } else {
            loading.close()
            location.reload()
          }
        })
      })
        .finally(() => {
          loading.close()
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.tools-li {
  width: 100%;
  height: 100%;
  position: relative;
}
.custom-demo {
  position: absolute;
  top: 60px;
  right: 0;
  height: 140px;
  width: 200px;
  z-index: 100;
  border-radius: 4px;
}
.custom-body {
  width: 100%;
  height: 100%;
  position: relative;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding: 10px;
  box-sizing: border-box;
  display: flex;
  flex-flow: column nowrap;
  background-color: var(--edu-bk-100);
}
.custom-header {
  flex: 0 1 auto;
  height: 80px;
  display: flex;
  flex-flow: row nowrap;
}
.custom-button {
  flex: 0 1 auto;
  height: 40px;
  text-align: left;
  background-color: var(--edu-bk-100);
}
.user-image {
  flex: 0 1 auto;
  width: 70px;
  height: 80px;
  padding: 10px 0;
  box-sizing: border-box;
}
.user-info {
  height: 80px;
  flex: 1 1 auto;
  padding: 10px 0px;
  box-sizing: border-box;
}
.user-info-li {
  height: 30px;
  line-height: 30px;
  text-align: left;
  width: 100%;
  color: var(--edu-text-200);
}
.user-tx {
  height: 60px;
  margin: 0 auto;
}
.image-slot {
  width: 50px;
  height: 60px;
}
.custom-button-color {
  color: var(--edu-hover-text-100);
}
.custom-iframe {
  width: 100%;
  display: none;
}
</style>
